package com.lifeng.patterns.spring;

import com.lifeng.patterns.spring.controller.SpringController;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @description:类初始化完成后执行
 * @author: lifeng
 * @create: 2020/9/21 16:28
 * 使用spring中的InitializingBean接口，监听spring的启动过程
 * 在spring装载完所有的bean的时候，会自动调用实现InitializingBean的afterPropertiesSet（）方法，
 * 在里面去调用初始化用的服务或者逻辑就行
 */
@Component
public class SystemInit implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        SpringController c = ApplicationContextUtils.applicationContext.getBean(SpringController.class);

        //动态注册一个bean
        GenericApplicationContext genericApplicationContext = (GenericApplicationContext) ApplicationContextUtils.applicationContext;
        //genericApplicationContext.registerBean(SysUser.class.getName(),SysUser.class,() -> initSysUser(),new BeanDefinitionCustomizer[0]);
    }

    public SysUser initSysUser(){
        SysUser user = new SysUser();
        user.setId("1222");
        user.setName("账上");
        return user;
    }
}
